package com.qt.thread;

import lombok.SneakyThrows;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author zsf
 * @Date 2023/10/2 17:56
 * @Version 1.0
 */
public class MyThreadTicket extends Thread {

    private  static int ticket = 0;
    private static Object o1 = new Object();
    static Lock lock = new ReentrantLock();

    public MyThreadTicket() {
    }

    public MyThreadTicket(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (true){
            //synchronized (o1){
            //synchronized (MyThreadTicket.class){
            lock.lock();
            try {
                if (ticket < 100){
                     Thread.sleep(10);
                    System.out.println(Thread.currentThread().getName() + "卖第" + ticket + "张票");
                    ticket++;
                }else {
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            //}
        }
    }
}
